package com.noahplat.person.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.noahplat.common.utils.NoahEnum;
import com.noahplat.common.utils.RedisUtils;
import com.noahplat.common.utils.Result;
import com.noahplat.common.utils.StatusCode;
import com.noahplat.person.mapper.DiseaseMapper;
import com.noahplat.person.mapper.LifeMapper;
import com.noahplat.person.pojo.Disease;
import com.noahplat.person.pojo.Life;
import com.noahplat.person.service.DiseaseService;
import io.seata.spring.annotation.GlobalTransactional;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: DiseaseServiceImpl
 * @Description: 疾病枚举实现类
 * @Author 李景星
 * @Date 2021/3/5
 * @Version 1.0
 */
@Service
public class DiseaseServiceImpl implements DiseaseService {

    @Autowired
    private DiseaseMapper diseaseMapper;
    @Autowired
    private LifeMapper lifeMapper;
    @Autowired
    private RedisUtils redisUtils;


    /**
     * 获取所有疾病字典
     * @return
     */
    @Override
    public Result getAllDisease() {
        //先去redis中获取，若redis中有数据，则从redis中读取，若没有，从数据库表查询，然后在放入redis
        if(redisUtils.hasKey(NoahEnum.DISEATEDIR.getCode()+ NoahEnum.DISEATEKEY.getCode())){
            List<Disease> list = (List<Disease>)(List) redisUtils.lGet(NoahEnum.DISEATEDIR.getCode() + NoahEnum.DISEATEKEY.getCode(), 0, -1);
            return new Result(true, StatusCode.OK, "查询疾病字典成功", list);
        }
        List<Disease> diseases = diseaseMapper.selectAll();
        if(ObjectUtils.isEmpty(diseases)){
            return new Result(false, StatusCode.NOTFOUNDERROR, "暂无疾病字典数据");
        }
        //数据存入redis
        for (Disease disease : diseases) {
            redisUtils.lSet(NoahEnum.DISEATEDIR.getCode() + NoahEnum.DISEATEKEY.getCode(), disease);//新数据存入redis
        }
        return new Result(true, StatusCode.OK, "查询疾病字典成功", diseases);
    }

    /**
     * 添加疾病字典
     * @param disease
     * @return
     */
    @Transactional
    @Override
    public Result addDisease(Disease disease){
        int res = diseaseMapper.insertSelective(disease);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
        boolean inres = redisUtils.lSet(NoahEnum.DISEATEDIR.getCode() + NoahEnum.DISEATEKEY.getCode(), disease);
        if(!inres){
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
        return new Result(true,StatusCode.OK,"添加成功");
    }


    /**
     * 分页获取疾病字典
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Result getAdminDisease(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Disease> list = list = diseaseMapper.selectAll();
        if(ObjectUtils.isEmpty(list)){
            return new Result(false,StatusCode.ERROR,"暂无数据");
        }
        PageInfo<Disease> pageInfo = new PageInfo<>(list);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /**
     * 获取疾病字典详细信息
     * @param id
     * @return
     */
    @Override
    public Result getDiseaseById(Long id){
        List<Disease> list = new ArrayList<Disease>();
        if(redisUtils.hasKey(NoahEnum.DISEATEDIR.getCode() + NoahEnum.DISEATEKEY.getCode())){
            list = (List<Disease>)(List)redisUtils.lGet(NoahEnum.DISEATEDIR.getCode() + NoahEnum.DISEATEKEY.getCode(), 0, -1);
        }else {
            list = diseaseMapper.selectAll();
            for (Disease disease : list) {
                redisUtils.lSet(NoahEnum.DISEATEDIR.getCode() + NoahEnum.DISEATEKEY.getCode(), disease);

            }
        }
        for(Disease disease : list){
            if(disease.getId() == id){
                return new Result(true,StatusCode.OK,"查询成功",disease);
            }
        }
        return new Result(false,StatusCode.ERROR,"暂无数据");
    }
}
